//import UIKit
//
//class ViewController: UIViewController {
//    
//    // MARK: - 控件属性
//    private var textField: UITextField!
//    private var passwordField: UITextField!
//    private var normalButton: UIButton!
//    private var toggleButton: UIButton!
//    private var label: UILabel!
//    private var switchControl: UISwitch!
//    private var slider: UISlider!
//    private var stepper: UIStepper!
//    private var segmentedControl: UISegmentedControl!
//    private var pickerView: UIPickerView!
//    private var datePicker: UIDatePicker!
//    private var progressView: UIProgressView!
//    private var activityIndicator: UIActivityIndicatorView!
//    private var tableView: UITableView!
//    private var collectionView: UICollectionView!
//    
//    // 数据
//    private let pickerData = ["选项1", "选项2", "选项3", "选项4", "选项5"]
//    private let tableData = Array(1...20).map { "表格行 \($0)" }
//    
//    override func viewDidLoad() {
//        super.viewDidLoad()
//        setupUI()
//        setupConstraints()
//        setupActions()
//    }
//    
//    // MARK: - UI设置
//    private func setupUI() {
//        view.backgroundColor = .systemBackground
//        
//        // 创建导航栏
//        navigationItem.title = "UIKit控件示例"
//        navigationController?.navigationBar.prefersLargeTitles = true
//        
//        // 文本输入框
//        textField = UITextField()
//        textField.placeholder = "请输入文本"
//        textField.borderStyle = .roundedRect
//        textField.keyboardType = .default
//        textField.returnKeyType = .done
//        textField.clearButtonMode = .whileEditing
//        view.addSubview(textField)
//        
//        // 密码输入框
//        passwordField = UITextField()
//        passwordField.placeholder = "请输入密码"
//        passwordField.borderStyle = .roundedRect
//        passwordField.isSecureTextEntry = true
//        passwordField.returnKeyType = .done
//        view.addSubview(passwordField)
//        
//        // 普通按钮
//        normalButton = UIButton(type: .system)
//        normalButton.setTitle("点击按钮", for: .normal)
//        normalButton.titleLabel?.font = UIFont.systemFont(ofSize: 16)
//        normalButton.layer.cornerRadius = 8
//        normalButton.backgroundColor = .systemBlue
//        normalButton.tintColor = .white
//        normalButton.contentEdgeInsets = UIEdgeInsets(top: 8, left: 16, bottom: 8, right: 16)
//        view.addSubview(normalButton)
//        
//        // 切换按钮
//        toggleButton = UIButton(type: .system)
//        toggleButton.setTitle("未选中", for: .normal)
//        toggleButton.setTitle("已选中", for: .selected)
//        toggleButton.titleLabel?.font = UIFont.systemFont(ofSize: 16)
//        toggleButton.layer.cornerRadius = 8
//        toggleButton.layer.borderWidth = 1
//        toggleButton.layer.borderColor = UIColor.systemBlue.cgColor
//        toggleButton.tintColor = .systemBlue
//        toggleButton.contentEdgeInsets = UIEdgeInsets(top: 8, left: 16, bottom: 8, right: 16)
//        view.addSubview(toggleButton)
//        
//        // 标签
//        label = UILabel()
//        label.text = "标签文本"
//        label.font = UIFont.systemFont(ofSize: 16)
//        label.textColor = .label
//        label.textAlignment = .center
//        label.numberOfLines = 0
//        view.addSubview(label)
//        
//        // 开关
//        switchControl = UISwitch()
//        switchControl.isOn = true
//        switchControl.onTintColor = .systemGreen
//        view.addSubview(switchControl)
//        
//        // 滑块
//        slider = UISlider()
//        slider.minimumValue = 0
//        slider.maximumValue = 100
//        slider.value = 50
//        slider.minimumTrackTintColor = .systemBlue
//        slider.maximumTrackTintColor = .systemGray3
//        view.addSubview(slider)
//        
//        // 步进器
//        stepper = UIStepper()
//        stepper.minimumValue = 0
//        stepper.maximumValue = 100
//        stepper.value = 50
//        stepper.stepValue = 1
//        view.addSubview(stepper)
//        
//        // 分段控件
//        segmentedControl = UISegmentedControl(items: ["选项1", "选项2", "选项3"])
//        segmentedControl.selectedSegmentIndex = 0
//        segmentedControl.tintColor = .systemBlue
//        view.addSubview(segmentedControl)
//        
//        // 选择器
//        pickerView = UIPickerView()
//        pickerView.delegate = self
//        pickerView.dataSource = self
//        view.addSubview(pickerView)
//        
//        // 日期选择器
//        datePicker = UIDatePicker()
//        datePicker.datePickerMode = .date
//        datePicker.preferredDatePickerStyle = .wheels
//        datePicker.locale = Locale(identifier: "zh_CN")
//        view.addSubview(datePicker)
//        
//        // 进度条
//        progressView = UIProgressView(progressViewStyle: .default)
//        progressView.progress = 0.5
//        progressView.tintColor = .systemBlue
//        progressView.trackTintColor = .systemGray3
//        view.addSubview(progressView)
//        
//        // 活动指示器
//        activityIndicator = UIActivityIndicatorView(style: .medium)
//        activityIndicator.color = .systemBlue
//        activityIndicator.startAnimating()
//        view.addSubview(activityIndicator)
//        
//        // 表格视图
//        tableView = UITableView()
//        tableView.delegate = self
//        tableView.dataSource = self
//        tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell")
//        tableView.tableFooterView = UIView() // 隐藏多余的分隔线
//        view.addSubview(tableView)
//        
//        // 集合视图
//        let layout = UICollectionViewFlowLayout()
//        layout.itemSize = CGSize(width: (view.bounds.width - 40) / 3, height: 50)
//        layout.minimumInteritemSpacing = 10
//        layout.minimumLineSpacing = 10
//        layout.sectionInset = UIEdgeInsets(top: 10, left: 10, bottom: 10, right: 10)
//        
//        collectionView = UICollectionView(frame: .zero, collectionViewLayout: layout)
//        collectionView.delegate = self
//        collectionView.dataSource = self
//        collectionView.register(UICollectionViewCell.self, forCellWithReuseIdentifier: "collectionCell")
//        collectionView.backgroundColor = .systemBackground
//        view.addSubview(collectionView)
//    }
//    
//    // MARK: - 约束设置
//    private func setupConstraints() {
//        textField.translatesAutoresizingMaskIntoConstraints = false
//        passwordField.translatesAutoresizingMaskIntoConstraints = false
//        normalButton.translatesAutoresizingMaskIntoConstraints = false
//        toggleButton.translatesAutoresizingMaskIntoConstraints = false
//        label.translatesAutoresizingMaskIntoConstraints = false
//        switchControl.translatesAutoresizingMaskIntoConstraints = false
//        slider.translatesAutoresizingMaskIntoConstraints = false
//        stepper.translatesAutoresizingMaskIntoConstraints = false
//        segmentedControl.translatesAutoresizingMaskIntoConstraints = false
//        pickerView.translatesAutoresizingMaskIntoConstraints = false
//        datePicker.translatesAutoresizingMaskIntoConstraints = false
//        progressView.translatesAutoresizingMaskIntoConstraints = false
//        activityIndicator.translatesAutoresizingMaskIntoConstraints = false
//        tableView.translatesAutoresizingMaskIntoConstraints = false
//        collectionView.translatesAutoresizingMaskIntoConstraints = false
//        
//        // 使用UIStackView组织控件，简化布局
//        let stackView = UIStackView()
//        stackView.axis = .vertical
//        stackView.spacing = 20
//        stackView.alignment = .fill
//        stackView.distribution = .fillProportionally
//        stackView.translatesAutoresizingMaskIntoConstraints = false
//        view.addSubview(stackView)
//        
//        // 添加控件到堆栈视图
//        stackView.addArrangedSubview(textField)
//        stackView.addArrangedSubview(passwordField)
//        stackView.addArrangedSubview(normalButton)
//        stackView.addArrangedSubview(toggleButton)
//        stackView.addArrangedSubview(label)
//        stackView.addArrangedSubview(switchControl)
//        stackView.addArrangedSubview(slider)
//        stackView.addArrangedSubview(stepper)
//        stackView.addArrangedSubview(segmentedControl)
//        stackView.addArrangedSubview(pickerView)
//        stackView.addArrangedSubview(datePicker)
//        stackView.addArrangedSubview(progressView)
//        stackView.addArrangedSubview(activityIndicator)
//        stackView.addArrangedSubview(tableView)
//        stackView.addArrangedSubview(collectionView)
//        
//        // 设置堆栈视图的约束
//        NSLayoutConstraint.activate([
//            stackView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor, constant: 20),
//            stackView.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 20),
//            stackView.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -20),
//            stackView.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor, constant: -20)
//        ])
//        
//        // 设置各个控件的高度约束
//        NSLayoutConstraint.activate([
//            textField.heightAnchor.constraint(equalToConstant: 44),
//            passwordField.heightAnchor.constraint(equalToConstant: 44),
//            normalButton.heightAnchor.constraint(equalToConstant: 44),
//            toggleButton.heightAnchor.constraint(equalToConstant: 44),
//            label.heightAnchor.constraint(equalToConstant: 44),
//            switchControl.heightAnchor.constraint(equalToConstant: 31), // UISwitch的默认高度
//            slider.heightAnchor.constraint(equalToConstant: 31),
//            stepper.heightAnchor.constraint(equalToConstant: 31),
//            segmentedControl.heightAnchor.constraint(equalToConstant: 31),
//            pickerView.heightAnchor.constraint(equalToConstant: 150),
//            datePicker.heightAnchor.constraint(equalToConstant: 150),
//            progressView.heightAnchor.constraint(equalToConstant: 2),
//            activityIndicator.heightAnchor.constraint(equalToConstant: 30),
//            tableView.heightAnchor.constraint(equalToConstant: 200),
//            collectionView.heightAnchor.constraint(equalToConstant: 150)
//        ])
//    }
//    
//    // MARK: - 动作设置
//    private func setupActions() {
//        // 按钮点击事件
//        normalButton.addTarget(self, action: #selector(buttonTapped), for: .touchUpInside)
//        toggleButton.addTarget(self, action: #selector(toggleButtonTapped), for: .touchUpInside)
//        
//        // 开关状态变化事件
//        switchControl.addTarget(self, action: #selector(switchValueChanged), for: .valueChanged)
//        
//        // 滑块值变化事件
//        slider.addTarget(self, action: #selector(sliderValueChanged), for: .valueChanged)
//        
//        // 步进器值变化事件
//        stepper.addTarget(self, action: #selector(stepperValueChanged), for: .valueChanged)
//        
//        // 分段控件值变化事件
//        segmentedControl.addTarget(self, action: #selector(segmentedControlValueChanged), for: .valueChanged)
//        
//        // 日期选择器值变化事件
//        datePicker.addTarget(self, action: #selector(datePickerValueChanged), for: .valueChanged)
//        
//        // 文本输入框代理
//        textField.delegate = self
//        passwordField.delegate = self
//    }
//    
//    // MARK: - 事件处理
//    @objc private func buttonTapped() {
//        let alert = UIAlertController(title: "按钮点击", message: "普通按钮被点击了", preferredStyle: .alert)
//        alert.addAction(UIAlertAction(title: "确定", style: .default))
//        present(alert, animated: true)
//    }
//    
//    @objc private func toggleButtonTapped() {
//        toggleButton.isSelected.toggle()
//        if toggleButton.isSelected {
//            toggleButton.backgroundColor = .systemBlue
//            toggleButton.tintColor = .white
//        } else {
//            toggleButton.backgroundColor = .clear
//            toggleButton.tintColor = .systemBlue
//        }
//    }
//    
//    @objc private func switchValueChanged() {
//        label.text = "开关状态: \(switchControl.isOn ? "开启" : "关闭")"
//    }
//    
//    @objc private func sliderValueChanged() {
//        label.text = "滑块值: \(Int(slider.value))"
//        progressView.progress = slider.value / 100.0
//    }
//    
//    @objc private func stepperValueChanged() {
//        label.text = "步进器值: \(Int(stepper.value))"
//    }
//    
//    @objc private func segmentedControlValueChanged() {
//        label.text = "选中的分段: \(segmentedControl.titleForSegment(at: segmentedControl.selectedSegmentIndex) ?? "")"
//    }
//    
//    @objc private func datePickerValueChanged() {
//        let dateFormatter = DateFormatter()
//        dateFormatter.dateStyle = .medium
//        dateFormatter.timeStyle = .none
//        dateFormatter.locale = Locale(identifier: "zh_CN")
//        let dateString = dateFormatter.string(from: datePicker.date)
//        label.text = "选择的日期: \(dateString)"
//    }
//}
//
//// MARK: - UITableViewDelegate, UITableViewDataSource
//extension ViewController: UITableViewDelegate, UITableViewDataSource {
//    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
//        return tableData.count
//    }
//    
//    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
//        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
//        cell.textLabel?.text = tableData[indexPath.row]
//        cell.accessoryType = .disclosureIndicator
//        return cell
//    }
//    
//    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
//        tableView.deselectRow(at: indexPath, animated: true)
//        let alert = UIAlertController(title: "表格选择", message: "选中了: \(tableData[indexPath.row])", preferredStyle: .alert)
//        alert.addAction(UIAlertAction(title: "确定", style: .default))
//        present(alert, animated: true)
//    }
//}
//
//// MARK: - UICollectionViewDelegate, UICollectionViewDataSource
//extension ViewController: UICollectionViewDelegate, UICollectionViewDataSource {
//    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
//        return 12
//    }
//    
//    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
//        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "collectionCell", for: indexPath)
//        cell.backgroundColor = .systemBlue
//        cell.layer.cornerRadius = 8
//        
//        // 添加标签
//        let label = UILabel(frame: cell.contentView.bounds)
//        label.text = "项目 \(indexPath.item + 1)"
//        label.textAlignment = .center
//        label.textColor = .white
//        label.font = UIFont.systemFont(ofSize: 14)
//        label.tag = 100
//        
//        // 避免重复添加标签
//        if cell.contentView.viewWithTag(100) == nil {
//            cell.contentView.addSubview(label)
//        }
//        
//        return cell
//    }
//    
//    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
//        let alert = UIAlertController(title: "集合视图选择", message: "选中了项目: \(indexPath.item + 1)", preferredStyle: .alert)
//        alert.addAction(UIAlertAction(title: "确定", style: .default))
//        present(alert, animated: true)
//    }
//}
//
//// MARK: - UIPickerViewDelegate, UIPickerViewDataSource
//extension ViewController: UIPickerViewDelegate, UIPickerViewDataSource {
//    func numberOfComponents(in pickerView: UIPickerView) -> Int {
//        return 1
//    }
//    
//    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
//        return pickerData.count
//    }
//    
//    func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
//        return pickerData[row]
//    }
//    
//    func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
//        label.text = "选择的项: \(pickerData[row])"
//    }
//}
//
//// MARK: - UITextFieldDelegate
//extension ViewController: UITextFieldDelegate {
//    func textFieldShouldReturn(_ textField: UITextField) -> Bool {
//        textField.resignFirstResponder()
//        if textField == self.textField {
//            label.text = "输入的文本: \(textField.text ?? "")"
//        } else if textField == self.passwordField {
//            label.text = "密码已输入"
//        }
//        return true
//    }
//}
//
//// 应用程序入口点
//@UIApplicationMain
//class AppDelegate: UIResponder, UIApplicationDelegate {
//    var window: UIWindow?
//    
//    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
//        window = UIWindow(frame: UIScreen.main.bounds)
//        
//        let navigationController = UINavigationController(rootViewController: ViewController())
//        window?.rootViewController = navigationController
//        window?.makeKeyAndVisible()
//        
//        return true
//    }
//}
